package com.tk.exam.web.restful;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.util.StringUtil;
import com.tk.exam.domain.TkStudent;
import com.tk.exam.domain.TkToken;
import com.tk.exam.exception.ApiException;
import com.tk.exam.http.Response;
import com.tk.exam.interceptor.NoCheckHttpHeader;
import com.tk.exam.service.TkStudentServer;
import com.tk.exam.service.TkTokenService;
import com.tk.exam.utils.DESUtil;
import com.tk.exam.utils.SSOUtil;
import com.tk.exam.utils.TokenThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@RestController
@RequestMapping("tk/exam/restful")
@Slf4j
public class LoginController {


    @Autowired
    TkTokenService tkTokenService;
    @Autowired
    TkStudentServer tkStudentServer;
    @Autowired
    SSOUtil ssoUtil;
    @Autowired
    TokenThreadUtil tokenThreadUtil;


    @PostMapping("/login")
    @NoCheckHttpHeader
    public Response login(@RequestBody Map<String, String> params) {
        if (params == null || params.size() == 0) {
            throw ApiException.of("用户名或密码不能为空");
        }
        String username = params.get("username");
        String password = params.get("password");

        //参数校验
        if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password)) {
            throw ApiException.of("用户名或密码不能为空");
        }
        //校验密码
        password = DESUtil.getEncryptPassword(password);
        List<TkStudent> tkStudents = tkStudentServer.getByLoginName(username);
        if (tkStudents.size()==0) {
            throw ApiException.of("用户名或密码错误");
        }

        TkStudent tkStudent;
        if(tkStudents.size()==1){
            tkStudent = tkStudents.get(0);
        }else{//查到考生在多个班级
            tkStudent = tkStudentServer.getStudentByAll(tkStudents);
        }

        if (!password.equals(tkStudent.getPassword())) {
            throw ApiException.of("用户名或密码错误");
        }
        //业务校验
        Map<String, Object> resultMap = tkStudentServer.validateLoginBusiness(tkStudent);
//        if(!resultMap.get("type").equals(4)){
//            return Response.succeed(resultMap);
//        }
        //保存token
        Map tokenInfoMap = new HashMap();
        tokenInfoMap.put("loginType", "restful");
        tokenInfoMap.put("userId", tkStudent.getId());
        tokenInfoMap.put("username", tkStudent.getPhoneNo());
        tokenInfoMap.put("password", tkStudent.getPassword());
        tokenInfoMap.put("studentName", tkStudent.getStudentName());
        tokenInfoMap.put("classId", tkStudent.getClassId());
        String tokenInfo = JSON.toJSONString(tokenInfoMap);
        TkToken tkToken = ssoUtil.generateToken(tokenInfo);
        tkTokenService.saveToken(tkToken);

        //返回json
        resultMap.put("token", tkToken.getToken());
        resultMap.put("username",tkStudent.getStudentName());
        resultMap.put("phone",tkStudent.getPhoneNo());
        return Response.succeed(resultMap);
    }



    /**
     * 登出——删除token
     *
     * @return
     */
    @PostMapping("/logout")
    public Response logout() {
        TkToken tkToken = TokenThreadUtil.getTkToken();
        tkTokenService.removeToken(tkToken);
        return Response.succeed();
    }


}
